package com.mtwp.dfs.domain.category;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.twp.auth.domain.authority.Menu;
import com.twp.auth.domain.authority.user.ManageUser;
import lombok.*;
import lombok.experimental.Accessors;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.persistence.*;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;


@EqualsAndHashCode(
        exclude = {
                "childrenRef"
        }
)
@ToString(
        exclude = {
                "childrenRef"
        }
)
@AllArgsConstructor
@NoArgsConstructor
@Data
@Entity
@Table(
        name = "dfs_diskstore",
        indexes ={
                @Index(name="name_index", columnList="name"),
                //@Index(name="pathCode_index", columnList="pathCode"),
                //@Index(name="setid_jobcode_index", columnList="setid,jobcode")
        },
        uniqueConstraints = @UniqueConstraint(
                columnNames={"pathCode"}
        )
)
@Accessors(chain = true)
public class DiskStore {
    /**
     * 更新时间
     */
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    @Column(name = "updateTime")
    private Date updateTime;

    /**
     * 创建时间
     */
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    @Column(name = "createTime")
    private Date createTime;

    /**
     * 编号
     */
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Id
    private Integer id;

    //顺序
    private Integer disOrder;

    /*
    level层级
    子及别 ++
     */
    private Integer level = 1;

    /*
    路径code 暂定6位，每层的最大数量6位量级
    000001
    000001000002
    pathCode like 000001_____
     */
    private String pathCode;

    /*
    是否叶子节点
     */
    private boolean isLeaf;

    //下载地址
    private String downloadUrl;

    /**子组织*/
    @OrderBy("disOrder asc ")
    @OneToMany
    @JoinColumn(name="parent_id")
    @JsonIgnore
    private Set<DiskStore> childrenRef = new HashSet();


    @ManyToOne
    @JoinColumn(name = "manageUser_Id")
    @JsonIgnore
    private ManageUser manageUser;

    /*
    强拉取关联
     */
    @OneToOne(fetch = FetchType.EAGER)
    private FileType fileType;

    @Transient
    private Set<DiskStore> children = new HashSet();

    public Set<DiskStore> getChildren() {
        if (children==null) return null;
        if (children.isEmpty()){
            children = this.getChildrenRef();
        }
        return children;
    }

    /*
    状态 锁定0，正常1，逻辑删除-1，物理删除(直接删掉记录)
     */
    private Integer status = 1;

    /*
    容量
     */
    private String capacity;

    /*
    name 用于搜索，建立字段索引
     */
    private String name;

    /*
    prefix
    path
     */
    private String prefix;

    /*
    suffix 后缀  扩展
     */
    private String extension;

    /*
    完整地址
     */
    @Transient
    private String path;

    /*
    文件路径
     */
    private String path(){
        if (path==null) this.path = new StringBuffer(prefix==null?"":prefix).append(name==null?"":name).append(extension==null?"":extension).toString();
        return path;
    }

    /*
    描述
     */
    private String description;


    @Transient
    @JsonIgnore
    private MultipartFile file;

}
